package org.windowkit.toolkit.util;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JPanel;

import org.windowkit.toolkit.widget.ComplexComponent;


/**
 * A utility with convenience methods for laying out panels. A work in progress
 *
 * @author Rich
 */
public class LayoutUtils {

	/** Insets to use for components. */
	protected static final Insets componentInsets = new Insets(2, 2, 2, 2);

    public static JPanel createPanel(final String title, final JComponent[] ... comps){
        return createPanel(title, 0, comps);
    }

    //XXX space doesnt really work if your going to using insertIntoGrid later
    public static  JPanel createPanel(final String title, final int space, final JComponent[]... comps){

            JPanel panel = new JPanel(new GridBagLayout());
            if(title != null) {
				panel.setBorder(BorderFactory.createTitledBorder(title));
			}

            GridBagConstraints gbc = new GridBagConstraints();

//            gbc.anchor = GridBagConstraints.WEST;
            gbc.anchor = GridBagConstraints.EAST;
            gbc.weighty = 1;

//            gbc.fill = GridBagConstraints.NONE;
//            gbc.weightx = 1;

            for(int i = 0; i < comps.length; i++){

                gbc.insets = new Insets(5, 5, 5, 5);

                JComponent[] column = comps[i];

                gbc.gridx = i;
                //labels
                if(i%2 == 0){
                    gbc.fill = GridBagConstraints.NONE;
                    gbc.weightx = 1;
                }
                //components
                else{
                    gbc.fill = GridBagConstraints.HORIZONTAL;
                    gbc.weightx = 0;
                    if(space > 0 && i != comps.length - 1 ){
                        gbc.insets.right += space;
                    }
                }

                for(int j = 0; j < column.length; j++){
                   gbc.gridy = j;
                   if(column[j] != null) {
					panel.add(column[j], gbc);
				}
                }

            }
            return panel;
        }

    public static void insertIntoGrid(final JPanel panel, final JComponent comp,
            final int x, final int y, final int width, final int height){

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        //        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(comp, gbc);

    }

    /**
     * Insert the ComplexComponent into the panel at location x,y.
     * The ComplexComponent's gridWidth and gridHeight members are used
     * to determine its size.  Panel should be laid out using grid bag layout
     * and have open grid space at the desired location.
     *
     * @param panel
     * @param comp
     * @param x
     * @param y
     */
    public static void insertIntoGrid(final JPanel panel, final ComplexComponent comp, final int x, final int y){
        insertIntoGrid(panel, comp, x, y, comp.getGridWidth(), comp.getGridHeight());
    }




    public static void adjustPanelWidth(final JPanel panel, final int width){
        Dimension dim = panel.getPreferredSize();
        dim.width = width;
        panel.setPreferredSize(dim);
    }

	/**
	 * Create a set of contraints which will fill in both directions.
	 *
	 * @return A set of constraints which will fill in both directions.
	 */
	public static GridBagConstraints createConstraintsFillBoth() {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = componentInsets;
		gbc.weightx = 1;
		gbc.weighty = 1;
		return gbc;
	}

	/**
	 * Create a set of contraints for a basic component such as a button.
	 *
	 * @return A set of constraints which will fill in both directions.
	 */
	public static GridBagConstraints createBasicConstraints() {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = componentInsets;
		return gbc;
	}

	/**
	 * Create a set of contraints which will fill in the X direction.
	 *
	 * @return A set of constraints which will fill in the X direction.
	 */
	public static GridBagConstraints createConstraintsFillX() {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = componentInsets;
		gbc.weightx = 1;
		return gbc;
	}

	/**
	 * Create a set of contraints which will fill in the Y direction.
	 *
	 * @return A set of constraints which will fill in the Y direction.
	 */
	public static GridBagConstraints createConstraintsFillY() {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.VERTICAL;
		gbc.insets = componentInsets;
		gbc.weighty = 1;
		return gbc;
	}

	/**
	 * Retrieve a strut which is the distance between 2 buttons.
	 *
	 * @return A component to force the given amount of space between 2 buttons.
	 */
	public static Component createButtonStrut() {
		return Box.createHorizontalStrut(componentInsets.left
				+ componentInsets.right);
	}

}
